package com.zhuxin.conf;

import com.dbHelper.DbTemplate;
import com.zhuxin.conf.prop.DataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class AtomikosConfiguration {

	@Autowired
	private DataSourceProperties properties;

	@Autowired
	private ApplicationContext applicationContext;

	@Bean
	public Map<String,DataSource> dataSources() throws SQLException {
		Map<String,DataSource> rsmap=new HashMap<>(16);
		for (int i = 0; i < properties.getSourcename().length; i++) {
			AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
			dataSource.setUniqueResourceName(properties.getSourcename()[i]);
			dataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
			dataSource.setPoolSize(properties.getInitSize()[i]);//初始化连接数
			dataSource.setMinPoolSize(properties.getMinIdle()[i]);//最少空闲连接数
			dataSource.setMaxPoolSize(properties.getMaxActSize()[i]);//最大连接数
			dataSource.setReapTimeout(properties.getQueryTimeout()[i]);//查询超时
//			dataSource.setTransactionQueryTimeout(properties.getTransactionQueryTimeout()[i]);//事务超时
			dataSource.setTestQuery(properties.getValidationQuery()[i]);//检测SQL
			dataSource.setLoginTimeout(30);//登录超时
			Properties p=new Properties();
			p.setProperty("url", properties.getUrl()[i]);
			p.setProperty("username", properties.getUsername()[i]);
			p.setProperty("password", properties.getPassword()[i]);
			p.setProperty("queryTimeout",properties.getTransactionQueryTimeout()[i].toString());
			p.setProperty("minIdle",properties.getMinIdle()[i].toString());
			p.setProperty("maxActive",properties.getMaxActSize()[i].toString());
			p.setProperty("filters", "stat,wall");
			p.setProperty("testWhileIdle","true");
			p.setProperty("testOnBorrow","true");
			p.setProperty("testOnReturn","false");
			p.setProperty("validationQuery",properties.getValidationQuery()[i]);
			dataSource.setXaProperties(p);
			rsmap.put(properties.getSourcename()[i],dataSource);
		}
		return rsmap;
	}

	@Bean
	public String dynamicConfiguration(Map<String,DataSource> dataSources){
		ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
		DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
		dataSources.forEach((n,d)->{
			DbTemplate dbTemplate=new DbTemplate();
			dbTemplate.setDataSource(d);
			dbTemplate.setDbType("mysql");
			beanFactory.registerSingleton(n+"Dao",dbTemplate);
		});
		return "";
	}
	
}
